package org.wlw.teacher.service.impl;

import org.wlw.teacher.anno.Resource;
import org.wlw.teacher.dao.IDepDao;
import org.wlw.teacher.dao.ITeacherDao;
import org.wlw.teacher.models.Department;
import org.wlw.teacher.models.Pager;
import org.wlw.teacher.models.Teacher;
import org.wlw.teacher.models.WebException;
import org.wlw.teacher.service.IDepartmentService;
import org.wlw.teacher.utils.WebUtils;

import java.util.List;

public class DepartmentService extends BaseService implements IDepartmentService {

    private IDepDao depDao;
    private ITeacherDao teacherDao;

    @Resource
    public void setTeacherDao(ITeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    @Resource
    public void setDepDao(IDepDao depDao) {
        this.depDao = depDao;
    }

    @Override
    public void add(Department dep) {
        depDao.add(dep);
    }

    @Override
    public void update(Department department) {
        depDao.update(department);
    }

    @Override
    public void delete(int id) {
        Teacher t = new Teacher();
        t.setDepId(id);
        List<Teacher> list = teacherDao.find(t);
        if(list.size()>0) {
            throw new WebException("不能删除部门，因为部门中还有教师!");
        }
        depDao.delete(id);
    }

    @Override
    public Department load(int id) {
        return depDao.load(id);
    }

    @Override
    public Pager<Department> find(Department dep) {
        if(dep==null) dep = new Department();
        //设置分页查询对象
        WebUtils.setPagerObject(dep);
        int totals = depDao.findByCount(dep);
        List<Department> deps = depDao.find(dep);
        Pager<Department> pager = new Pager<>();
        pager.setData(deps);
        //设置分页的返回对象
        WebUtils.setPager(pager,totals);
        return pager;
    }

    @Override
    public List<Department> list(Department dep) {
        return depDao.list(dep);
    }
}
